package com.ryder.ryderschool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ryder.ryderschool.entity.Project;
import com.ryder.ryderschool.entity.ProjectMember;
import com.ryder.ryderschool.entity.User;
import com.ryder.ryderschool.mapper.ProjectMemberMapper;
import com.ryder.ryderschool.service.ProjectMemberService;
import com.ryder.ryderschool.service.ProjectService;
import com.ryder.ryderschool.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ProjectMemberServiceImpl extends ServiceImpl<ProjectMemberMapper, ProjectMember> implements ProjectMemberService {

    private final ProjectService projectService;
    private final UserService userService;

    @Override
    @Transactional
    public void addMember(Integer projectId, Integer userId, String role) {
        // 检查项目是否存在
        Project project = projectService.getById(projectId);
        if (project == null) {
            throw new RuntimeException("项目不存在");
        }

        // 检查用户是否存在
        User user = userService.getById(userId);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }

        // 检查是否已经是项目成员
        if (lambdaQuery()
                .eq(ProjectMember::getProjectId, projectId)
                .eq(ProjectMember::getUserId, userId)
                .count() > 0) {
            throw new RuntimeException("该用户已经是项目成员");
        }

        // 添加成员
        ProjectMember member = new ProjectMember();
        member.setProjectId(projectId);
        member.setUserId(userId);
        member.setRole(role);
        save(member);
    }

    @Override
    @Transactional
    public void removeMember(Integer projectId, Integer userId) {
        // 检查是否是项目负责人
        Project project = projectService.getById(projectId);
        if (project != null && project.getLeaderId().equals(userId)) {
            throw new RuntimeException("不能移除项目负责人");
        }

        // 移除成员
        remove(new LambdaQueryWrapper<ProjectMember>()
                .eq(ProjectMember::getProjectId, projectId)
                .eq(ProjectMember::getUserId, userId));
    }

    @Override
    @Transactional
    public void updateMemberRole(Integer projectId, Integer userId, String role) {
        // 检查是否是项目负责人
        Project project = projectService.getById(projectId);
        if (project != null && project.getLeaderId().equals(userId)) {
            throw new RuntimeException("不能修改项目负责人的角色");
        }

        // 更新角色
        ProjectMember member = getOne(new LambdaQueryWrapper<ProjectMember>()
                .eq(ProjectMember::getProjectId, projectId)
                .eq(ProjectMember::getUserId, userId));
        if (member == null) {
            throw new RuntimeException("该用户不是项目成员");
        }
        member.setRole(role);
        updateById(member);
    }

    @Override
    public List<ProjectMember> getProjectMembers(Integer projectId) {
        return list(new LambdaQueryWrapper<ProjectMember>()
                .eq(ProjectMember::getProjectId, projectId));
    }
} 